﻿using ZeroSum.Models;

namespace ZeroSum.App.Datas;

/// <summary>
///     统一返回实体类型
/// </summary>
/// <typeparam name="T"></typeparam>
public class ApiResponse<T>
{
    protected ApiResponse(ResponseStatus code, string message, T data)
    {
        Code = (int)code;
        Message = message;
        Data = data;
    }

    private ApiResponse(int code, string message, T data)
    {
        Code = code;
        Message = message;
        Data = data;
    }

    /// <summary>
    ///     是否成功标记
    /// </summary>
    public bool Success => Code == (int)ResponseStatus.Success;

    public int Code { get; }
    public string Message { get; }
    public T Data { get; }

    public static ApiResponse<T> Of(ResponseStatus status, string message, T data)
    {
        return new ApiResponse<T>(status, message, data);
    }

    public static ApiResponse<T> Of(int code, string message, T data)
    {
        return new ApiResponse<T>(code, message, data);
    }

    public static ApiResponse<T> Of(T data)
    {
        return Of(ResponseStatus.Success, "请求成功", data);
    }
}

public class ApiResponse : ApiResponse<bool>
{
    protected ApiResponse(ResponseStatus code, string message, bool data)
        : base(code, message, data)
    {
    }

    public static ApiResponse Ok()
    {
        return new ApiResponse(ResponseStatus.Success, "请求成功!", true);
    }

    public static ApiResponse NotOk(string message)
    {
        return new ApiResponse(ResponseStatus.Error, message, false);
    }
}